using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using BaseAmd.Mmp;
using BaseAmd.Mmp.FileNameFormat;
using HslCommunication;
using HslCommunicationEx.OpcUa;
using HslCommunicationEx.OpcUa.Client.SieCnc;
using Opc.Ua;
using Catastrophe.Common.FileM;

namespace SiemensAmd.Mmp.OpcUa
{
	public class SieOpcUaProg : IProgHandler
	{
		private SieCncOpcUa _sieOpcUa;

		public const string Success = "Good";

		public Action<int, int, FileInfoEntity> DownLoadNcProcessHandler { get; set; }

		public Action<int, int> UploadPcProcessHandler { get; set; }

		public SieOpcUaProg()
		{
			this._sieOpcUa = new SieCncOpcUa();
		}

		public TransResult DeleteFile(List<string> fileAry, ProgApiResources apiRes)
		{
			OperateResult operateResult = this._sieOpcUa.MethodCallDeleteFile(fileAry.ToArray());
			return new TransResult
			{
				IsSuccess = operateResult.IsSuccess,
				Message = operateResult.Message
			};
		}

		public TransResult DownLoadNcMulti(List<FileInfoEntity> entityBuf, string remotePath, ProgApiResources apiRes)
		{
			StringBuilder stringBuilder = new StringBuilder();
			bool flag = true;
			List<UploadProgInfo> list = new List<UploadProgInfo>();
			foreach (FileInfoEntity item in entityBuf)
			{
				FileInfo fileInfo = new FileInfo(item.FullName);
				if (fileInfo.Extension.Trim() == "")
				{
					flag = false;
					StringBuilder stringBuilder2 = stringBuilder;
					StringBuilder.AppendInterpolatedStringHandler handler = new StringBuilder.AppendInterpolatedStringHandler(9, 2, stringBuilder2);
					handler.AppendLiteral("无效的文件类型");
					handler.AppendFormatted(item);
					handler.AppendLiteral("-空");
					handler.AppendFormatted(Environment.NewLine);
					stringBuilder2.Append(ref handler);
					continue;
				}
				string text = fileInfo.Name.Substring(0, fileInfo.Name.Length - fileInfo.Extension.Length);
				string text2 = fileInfo.Extension.Substring(1);
				using FileStream fileStream = new FileStream(item.FullName, FileMode.Open, FileAccess.Read);
				byte[] array = new byte[fileStream.Length];
				fileStream.Read(array, 0, array.Length);
				string partName = remotePath + "/" + item.Name.ToUpper();
				list.Add(new UploadProgInfo
				{
					MethodID = "/Methods/CopyFileToServer",
					PartName = partName,
					Data = array,
					Overwrite = true
				});
			}
			OperateResult operateResult = this._sieOpcUa.MethodCallCopyFileToServer(list.ToArray());
			return new TransResult
			{
				IsSuccess = (operateResult.IsSuccess && flag),
				Message = stringBuilder.ToString() + operateResult.Message
			};
		}

		public TransResult DownLoadNcSingle(FileInfoEntity srcFile, FileInfoEntity dstFileInfo, ProgApiResources apiRes)
		{
			return this.DownLoadNcMulti(new List<FileInfoEntity> { srcFile }, dstFileInfo.FullName, apiRes);
		}

		public BaseFileRename FileRename()
		{
			throw new NotImplementedException("OPC UA无实现接口");
		}

		public Tuple<TransResult, List<FileInfoEntity>> GetProgDetails(string stationName, ProgApiResources apiRes)
		{
			ReferenceDescriptionCollection referenceDescriptionCollection = this._sieOpcUa.QueryProgInfo("Part Program");
			List<FileInfoEntity> list = new List<FileInfoEntity>();
			foreach (ReferenceDescription item2 in referenceDescriptionCollection)
			{
				FileInfoEntity item = new FileInfoEntity
				{
					Name = item2.DisplayName.Text
				};
				list.Add(item);
			}
			return new Tuple<TransResult, List<FileInfoEntity>>(new TransResult
			{
				IsSuccess = (referenceDescriptionCollection.Count() > 0)
			}, list);
		}

		public bool IsFileExist(string fileName, ProgApiResources apiRes)
		{
			throw new NotImplementedException();
		}

		public TransResult Rename(string srcName, string destName, string root, ProgApiResources apiRes)
		{
			throw new NotImplementedException("OPC UA无实现接口");
		}

		public TransResult UploadPcSingle(FileInfoEntity remoteFileInfo, string localPath, ProgApiResources apiRes)
		{
			MethodCallResult methodCallResult = this._sieOpcUa.MethodCallCopyFileFromServer(remoteFileInfo.FullName);
			if (methodCallResult.status != "Good")
			{
				return new TransResult
				{
					IsSuccess = (methodCallResult.status == "Good"),
					Message = methodCallResult.ErrorMsg
				};
			}
			byte[] array = (byte[])methodCallResult.results.Value;
			using (FileStream fileStream = new FileStream(localPath, FileMode.Create, FileAccess.Write))
			{
				fileStream.Write(array, 0, array.Length);
			}
			return new TransResult
			{
				IsSuccess = true
			};
		}
	}
}
